const util = require('../../../utils/util.js')
const ana = require('../../../utils/dataAnalysi.js')

import aesJm from "../../../utils/aes.js"
Page({
    data:{
        "message": "Hello, MINI Program",
        id: 0,
        condition: true,
        flag: false,
        a: 1,
        b: 2,
        c: 3,
        name: "MINI",
        object:{
            key: "hello"
        },
        array: ['MINI'],
        zero: 0
    },
    assembleCMD(){
        const buffer = util.sendAckCommand()
        console.log(buffer)

        console.log(util.arrayToHexString(buffer))
        
        // const buffer = new ArrayBuffer(7)

        // console.log(`buffer: ${buffer}`)
        // const dataView = new DataView(buffer)
        // console.log(`dataView: ${dataView}`)

        // dataView.setUint8(0, Math.random() * 19| 0)
        // console.log(`dataView: ${dataView}`)
    },
    decryptData(){
        let key = "202050/C85002308"
        var keyBuffer = []
        for(var i = 0; i < key.length; i++){
            let keyValue = key.charCodeAt(i);
            keyBuffer.push(keyValue)
        }

        let completeOri = "49008d21a4865fe3a4f49d135e786174f3244c8609b83e9cae89cd99ddc435530383bfa8a2f92e8e26987aa23b6b8070f2f201b706948a58c11fc4bfc223ea38598e10a1375801fa662ed490398dd267cd975e8fc2ff645350fa44ab1804806a5ffa83ddb9f1ba1fe9328f2fad2bac472c9f44af396818be8b1322b81ae76503933f146c8df6862c1a94db4f81bf5873d5230b5e8f329a1055c3ccaa33f8f05a5c2a4ae9b98ac9b67a91553ffbd98db635c6bb85333e2756d032fe4ac46da1f875b84ce8be70a6e98ea73d67f7ccc3cff4dc21ac88839c75a29a1e59577f0044a4e354b1b8e0cf04d93f7ef8a81640388c5e2396"
        var decCompltetStr = ""
        let unEncStr = completeOri.slice(0,4)
        console.log("unEncStr:",unEncStr)
        decCompltetStr += unEncStr
        let encStr = completeOri.slice(4)
        console.log("encStr:",encStr)

        let decryptNum = parseInt(encStr.length/32);
        console.log("decryptNum:",decryptNum)
        let remainder = encStr.length % 32;
        console.log("remainder:",remainder)

        for (let index = 0; index < decryptNum; index++) {
            let section = encStr.slice(index*32, (index+1)*32)
            console.log("section:",section)

            let decryptSection = decryptData(section, keyBuffer)
            console.log("decryptSection:",decryptSection)

            decCompltetStr += decryptSection
        }
        if (remainder != 0) {
            let leftStr = encStr.slice(32*decryptNum, 32*decryptNum+remainder)
            console.log("leftStr:",leftStr)

            decCompltetStr += leftStr
        }
        console.log(decCompltetStr)
        
        let targetStr = "4900011f01116b621d7c9002c601000000000000000000000000000000000011144702000295ff4243410496e006430241c70109aa03b702ec04eb038703da02bb05de036b1f0334ce04a202c001fc017e820477c5048607e9038804b701d504ef029a04f7039702a004ca04dc028205d9028306e2044ecd018a03ef026aa102dc03198805df061db105c402e60320fe01a302fa039b05ff01b901c703a602f30126377dd3019f069201d70c950dfc0ca8025e88036f48e6038005f30275ad37ce3895058003e601e10d8b06be028c02c8048d07cd01c803fc019a05aa04f801d204f003c503b509c3071f2838c305af070b2396"
        if (decCompltetStr == targetStr) {
            console.log('解析正确')
        }

        return
        // let oriData = "8d21a4865fe3a4f49d135e786174f324"
        // console.log(oriData.length)
        // let targetData = "011f01116b621d7c9002c60100000000"
        // let key = "202050/C85002308"
        // var keyBuffer = []
        // for(var i = 0; i < key.length; i++){
        //     let keyValue = key.charCodeAt(i);
        //     keyBuffer.push(keyValue)
        // }
        // console.log(keyBuffer)
        // let decryptStr = decryptData(oriData,keyBuffer)
        // console.log(decryptStr)

        // let encStr = encryptionData(decryptStr,keyBuffer)
        // console.log(encStr)
        // if (decryptStr == targetData) {
        //     console.log("对比成功")

        // }
    },
    decompression_data(){
        var oriData = "fb1bfe03af038603b405f005ae038f0145f003d301830933 cf05da02 8a048002 1b11263f 3ac90883 02ed04a7 02de0127 78b4036590029801 ee01d602 82064948 9902c201 9d01d302 9c03d705 94018604 db0114d1 03e8013d a90549aa 01113389 0175377b 8d013c91099d0672 9e018004 b206fb02 8508c506 f906129a 089102bd 0104c901 64f20498 01e9011c af0174ee 017619fd 058904c0 0a9b02aa09c402e0 01ff04e4 011ea503 db03b501 7363a502 d905b004 fc079d02 8904c701 e4058b03 d60446aa 03dd0615 880105a1 01de01ac03d4013317de010914"
        oriData = oriData.replace(' ','')
        let oriBuffer = new Uint8Array(util.hexStrToArray(oriData))
        console.log(oriBuffer)

        let compression = ana.decompression_data(oriBuffer)
        console.log(compression)

    }
})
/**
 * AES-128 加密 
 * @param {type string} hexStr 加密前的16个字节的进制字符串  
 * @param {type array} keys 开锁器或者是锁的 keys 10进制数组
 * hexStr示例：06010101000000000000000000000000
 * keys示例：[20, 82, 47, 82, 54, 75, 63, 71, 48, 80, 65, 88, 17, 99, 45, 43]
 */
export const encryptionData = (hexStr, keys) => {
    if (keys === undefined) return console.error('encryptionData 方法keys必须传入');

    var byteKey = aesJm.CryptoJS.enc.int8array.parse(keys);
    var byteData = aesJm.CryptoJS.enc.Hex.parse(hexStr);
    var encrypt = aesJm.CryptoJS.AES.encrypt(byteData, byteKey, {
        mode: aesJm.CryptoJS.mode.ECB, // 加密模式
        padding: aesJm.CryptoJS.pad.NoPadding, //填充方式
        // iv: '', //偏移量
    });
    var encryptedStr = encrypt.ciphertext.toString();
    return encryptedStr;
}


/**
 * AES-128 解密
 */
export const decryptData = (hexStr, keys) => {
    if (keys === undefined) return console.error('decryptData 方法keys必须传入');

    var byteKey = aesJm.CryptoJS.enc.int8array.parse(keys);
    // console.log("byteKey:",byteKey)
    var byteData = aesJm.CryptoJS.enc.Hex.parse(hexStr);
    byteData = aesJm.CryptoJS.enc.Base64.stringify(byteData);
    // console.log("byteData:",byteData)

    var decrypt = aesJm.CryptoJS.AES.decrypt(byteData, byteKey, {
        mode: aesJm.CryptoJS.mode.ECB, // 加密模式
        padding: aesJm.CryptoJS.pad.NoPadding, //填充方式
        // iv: '', //偏移量
    });
    
    var decryptedStr = decrypt.toString(aesJm.CryptoJS.enc.Hex);
    // console.log("decryptedStr:",decryptedStr)
    return decryptedStr.toString();
}
